package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.UserLike;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.UserLikeVo;
import com.tanhua.dubbo.api.mongo.UserLikeApi;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Date;
import java.util.List;

@DubboService
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public void save(Long userId, Long likeUserId) {
//        判断是否存在
        if(! isExist(userId, likeUserId)){
            UserLike userLike = new UserLike();
            userLike.setId(new ObjectId());
            userLike.setUserId(userId);
            userLike.setLikeUserId(likeUserId);
            userLike.setCreated(new Date().getTime());
            mongoTemplate.save(userLike);
        }
    }

    @Override
    public boolean isExist(Long userId, Long likeUserId) {
//        2、判断对方是否喜欢自己 select * from user_like where userId= likeUserId and likeUserId=UserHolder.getUserId()
        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        return mongoTemplate.exists(query,UserLike.class);
    }

    @Override
    public List<UserLike> find(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        List<UserLike> userLikeList = mongoTemplate.find(query,UserLike.class);
        return userLikeList;
    }

    @Override
    public Integer getCount(Long userId) {
        Query query = new Query(Criteria.where("likeUserId").is(userId));
        long count = mongoTemplate.count(query,UserLike.class);
        return (int)count;
    }

    @Override
    public PageResult lovePage(Long userId, Integer page, Integer pagesize) {
        Query query = new Query(Criteria.where("userId").is(userId));
        query.limit(pagesize).skip((page-1)*pagesize).with(Sort.by(Sort.Direction.DESC,"created"));
        long count = mongoTemplate.count(query,UserLike.class);
        List<UserLike> userLikeList = mongoTemplate.find(query,UserLike.class);
        return new PageResult(page,pagesize,(int)count,userLikeList);
    }

    @Override
    public PageResult getBeingLovedPage(Long userId, Integer page, Integer pagesize) {
        Query query = new Query(Criteria.where("likeUserId").is(userId));
        query.limit(pagesize).skip((page-1)*pagesize).with(Sort.by(Sort.Direction.DESC,"created"));
        long count = mongoTemplate.count(query,UserLike.class);
        List<UserLike> userLikeList = mongoTemplate.find(query,UserLike.class);
        return new PageResult(page,pagesize,(int)count,userLikeList);
    }

    @Override
    public void deleteLove(Long userId, Long uid) {
        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(uid));
        mongoTemplate.remove(query,UserLike.class);
    }
}
